<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="along&#39;s blog">
  <meta name="keyword" content="hexo-theme, vuejs">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      mobx | ALONG
    
  </title>
  <link href="/cdn.along/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="/cdn.along/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="/cdn.along/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="/cdn.along/jquery/3.2.1/jquery.min.js"></script>
  <script src="/cdn.along/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="/cdn.along/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="/cdn.along/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>ALONG</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>mobx</h2>
  <p class="post-date">2021-01-14</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h3 id="mobx"><a href="#mobx" class="headerlink" title="mobx"></a>mobx</h3><h4 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h4><p>mobx需要项目支持装饰器语法，在<strong>create-react-app</strong>项目中，进行下面的操作</p>
<p>方式一:</p>
<pre><code>  1. npm run eject
  2. npm install @babel/plugin-proposal-decorators
  3. package.json

    &quot;babel&quot;: &#123;
        &quot;plugins&quot;: [
            [
                &quot;@babel/plugin-proposal-decorators&quot;,
                &#123;
                    &quot;legacy&quot;: true
                &#125;
            ]
        ]
    &#125;</code></pre>
<p>  方式二:</p>
<pre><code>  1. npm install react-app-rewired @babel/plugin-proposal-decorators customize-cra

  2. 在项目根目录下创建 config-overrides.js

      const &#123; override, addDecoratorsLegacy &#125; = require(&quot;customize-cra&quot;);

      module.exports = override(addDecoratorsLegacy());

  3. package.json

      &quot;scripts&quot;: &#123;
          &quot;start&quot;: &quot;react-app-rewired start&quot;,
          &quot;build&quot;: &quot;react-app-rewired build&quot;,
          &quot;test&quot;: &quot;react-app-rewired test&quot;,
      &#125;</code></pre>
<p>解决vscode编辑器关于装饰器语法的警告</p>
<p>  “javascript.implicitProjectConfig.experimentalDecorators”: true</p>
<h4 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i mobx mobx-react</span><br></pre></td></tr></table></figure>

<h4 id="上手使用"><a href="#上手使用" class="headerlink" title="上手使用"></a>上手使用</h4><p>这里通过一个简易的商品列表的功能，来演示<strong>mobx</strong>的使用</p>
<p>首先需要创建一下文件结构</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">src&#x2F;</span><br><span class="line">---- product&#x2F;</span><br><span class="line">	 ---- store&#x2F;</span><br><span class="line">	      ---- mobx.js</span><br><span class="line">	 ---- index.js</span><br><span class="line">---- index.js</span><br></pre></td></tr></table></figure>

<p>我们在<strong>mobx.js中</strong>创建一个<strong>store</strong> </p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;observable, action, computed &#125; <span class="keyword">from</span> <span class="string">&#x27;mobx&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Product</span> </span>&#123;</span><br><span class="line">	<span class="comment">// 通过observable修饰后的变量修改后才能被监听到</span></span><br><span class="line">	@observable list = [<span class="string">&#x27;商品1&#x27;</span>];</span><br><span class="line">	@observable isPicking = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 计算属性，当依赖项改变时重新计算计算属性值，需要加 get</span></span><br><span class="line">	@computed get totalNo () &#123;</span><br><span class="line">		<span class="keyword">const</span> temp = <span class="built_in">this</span>.list.filter(<span class="function"><span class="params">l</span> =&gt;</span> !l.isEat)</span><br><span class="line">		<span class="keyword">return</span> &#123;</span><br><span class="line">			num: temp.length,</span><br><span class="line">			weight: temp.reduce(<span class="function">(<span class="params">total, l</span>) =&gt;</span> &#123;<span class="keyword">return</span> total + l.weight&#125;, <span class="number">0</span>),</span><br><span class="line">			list: temp,</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	@computed get totalEat () &#123;</span><br><span class="line">		<span class="keyword">const</span> temp = <span class="built_in">this</span>.list.filter(<span class="function"><span class="params">l</span> =&gt;</span> l.isEat)</span><br><span class="line">		<span class="keyword">return</span> &#123;</span><br><span class="line">			num: temp.length,</span><br><span class="line">			weight: temp.reduce(<span class="function">(<span class="params">total, l</span>) =&gt;</span> &#123;<span class="keyword">return</span> total + l.weight&#125;, <span class="number">0</span>),</span><br><span class="line">			list: temp,</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 修饰用于修改属性值得方法，.bound 修饰才能保证this指向正确</span></span><br><span class="line">	@action.bound eat = <span class="function">(<span class="params">code</span>) =&gt;</span> &#123;</span><br><span class="line">		<span class="built_in">this</span>.list.find(<span class="function"><span class="params">l</span> =&gt;</span> l.code === code).isEat = <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	@action.bound addList = <span class="function">(<span class="params">name</span>) =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">		<span class="built_in">this</span>.list.push(&#123;</span><br><span class="line">			code: <span class="built_in">this</span>.code ++,</span><br><span class="line">			weight: <span class="built_in">parseInt</span>(<span class="built_in">Math</span>.random() * <span class="number">500</span>)</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实例并导出</span></span><br><span class="line"><span class="keyword">const</span> product = <span class="keyword">new</span> Product();</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> product;</span><br></pre></td></tr></table></figure>

<p>创建好的<strong>store</strong>需要在全局引入，并放入容器中方便子组件获取</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;Provider&#125; <span class="keyword">from</span> <span class="string">&#x27;mobx-react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> product <span class="keyword">from</span> <span class="string">&#x27;./product/store/mobx&#x27;</span>;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Provider product=&#123;product&#125;&gt;</span><br><span class="line">    &lt;App /&gt;</span><br><span class="line">  &lt;/Provider&gt;,</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">&#x27;root&#x27;</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>接下来我们在组件中尝试使用它</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; inject, observer &#125; <span class="keyword">from</span> <span class="string">&#x27;mobx-react&#x27;</span>;</span><br><span class="line"></span><br><span class="line">@inject(<span class="string">&#x27;product&#x27;</span>) <span class="comment">// 注入product这个store</span></span><br><span class="line">@observer  <span class="comment">// 当数据改变时会响应式通知组件更新</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">App</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">	<span class="keyword">const</span> &#123; list, addList, totalNo, totalEat, eat &#125; = <span class="built_in">this</span>.props.product;</span><br><span class="line">		<span class="keyword">return</span> (</span><br><span class="line">			&lt;div className=<span class="string">&quot;App&quot;</span>&gt;</span><br><span class="line">				&lt;h2&gt;苹果篮子&lt;/h2&gt;</span><br><span class="line">				&lt;hr/&gt;</span><br><span class="line">				&lt;div className=<span class="string">&quot;total&quot;</span>&gt;</span><br><span class="line">					&lt;div className=<span class="string">&quot;hastotal&quot;</span>&gt;</span><br><span class="line">						&lt;div&gt;当前&lt;/div&gt;</span><br><span class="line">						&lt;div&gt;&#123;totalNo.num&#125;个苹果，&#123;totalNo.weight&#125;克&lt;/div&gt;</span><br><span class="line">					&lt;/div&gt;</span><br><span class="line">					&lt;div className=<span class="string">&quot;nototal&quot;</span>&gt;</span><br><span class="line">						&lt;div&gt;已吃掉&lt;/div&gt;</span><br><span class="line">						&lt;div&gt;&#123;totalEat.num&#125;个苹果，&#123;totalEat.weight&#125;克&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">					&lt;/div&gt;</span><br><span class="line">				&lt;/div&gt;</span><br><span class="line">				&#123;totalNo.list.map(<span class="function"><span class="params">l</span> =&gt;</span> &#123;</span><br><span class="line">					<span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&quot;appleitem&quot;</span>&gt;</span></span></span><br><span class="line">						&lt;div className=&quot;button&quot;&gt;</span><br><span class="line">							&lt;button onClick=&#123;() =&gt; &#123;eat(l.code)&#125;&#125;&gt;吃掉&lt;/button&gt;</span><br><span class="line"><span class="xml">						<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">						&lt;div className=&quot;appleImage&quot;&gt;</span><br><span class="line">							&lt;img src=&#123;apple&#125; /&gt;</span><br><span class="line"><span class="xml">						<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">						&lt;div className=&quot;title&quot;&gt;</span><br><span class="line">							红苹果 - &#123;l.code&#125;号</span><br><span class="line"><span class="xml">						<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">						&lt;div className=&quot;wiehgt&quot;&gt;</span><br><span class="line">							&#123;l.weight&#125;克</span><br><span class="line"><span class="xml">						<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="xml">					<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">				&#125;)&#125;</span><br><span class="line">				&lt;p className=<span class="string">&quot;pickWrapper&quot;</span>&gt;</span><br><span class="line">					&lt;button className=<span class="string">&quot;pick&quot;</span> onClick=&#123;<span class="function">() =&gt;</span> &#123;</span><br><span class="line">						<span class="built_in">this</span>.props.product.addList(<span class="string">&#x27;商品&#x27;</span>)</span><br><span class="line">					&#125;&#125;&gt;添加一个商品&lt;/button&gt;</span><br><span class="line">				&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">			&lt;/div&gt;</span><br><span class="line">		);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> App;</span><br></pre></td></tr></table></figure>

<p>这样就可以看到<strong>list</strong>数据在页面中展示了</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#mobx" >
    <span class="tag-code">mobx</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2021/01/14/redux/">
        <span class="nav-arrow">← </span>
        
          redux
        
      </a>
    
    
      <a class="nav-right" href="/2021/01/20/react-hooks/">
        
          react-hooks
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#mobx"><span class="toc-nav-text">mobx</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%87%86%E5%A4%87"><span class="toc-nav-text">准备</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%AE%89%E8%A3%85"><span class="toc-nav-text">安装</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E4%B8%8A%E6%89%8B%E4%BD%BF%E7%94%A8"><span class="toc-nav-text">上手使用</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'https://github.com/alongithub/2021/01/14/mobx/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    Copyright&copy; 2019-2022 along
    <br>
    <a href="https://beian.miit.gov.cn/" target="_blank">京ICP备2020044791号-1</a>
    <!-- <br>
    Theme by <a href="https://github.com/alongithub">alongithub</a>
     -->
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("/cdn.along/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("/cdn.along/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->


<script src="/js/script.js"></script>


  </body>
</html>